<template>
    <div class="ChangeTable">

        <Row>
            <Col span="24">
                <Button type="primary" class="export-btn" @click="add('add')">新增</Button>
            </Col>
            <Table :loading="loading" border :columns="columns" :data="data" width="100%">
                <template slot-scope="{ row, index }" slot="costType">
                    <span>{{getTitle('exemptFeeType',row.costType)}}</span>
                </template>

                <template slot-scope="{ row }" slot="action">
                    <Button type="primary" size="small" @click="edit(row)"
                    >编辑
                    </Button>
                </template>
            </Table>
        </Row>
        <Row type="flex" justify="end" class="page">
            <Page :current="formValidate.pageNumber" :total="total" :page-size="formValidate.pageSize"
                  @on-change="changePage" @on-page-size-change="changePageSize" :page-size-opts="[10,20,50]"
                  size="small" show-total show-elevator show-sizer></Page>
        </Row>
        <addDepToExempt
            :isModalOpen="isModalOpen"
            :modalTitle="modalTitle"
            :baseData="rowParams"
            @closeModal="closeModal"
            :dataDict="dataDict"
            ref="addDepToExempt"
        />
    </div>
</template>
<script>
    import {
        getDepConfigListData,
        deleteExemptConfig,
        getDeptByDeptId
    } from "@/projects/basic/api/contract/fee-waiver/exempt-configuration/index";
    // import {
    //     getAllUserData
    // } from "@/projects/basic/api/admin/user";
    import {getTitleFromDataDict} from "_p/basic/assets/contract/js/utils";
    import addDepToExempt from "./addDepToExempt";
    import {getDictDataList} from "_p/basic/assets/contract/js/utils";
    import {deepClone} from "../../../../../../libs/utils/ObjectClone";

    export default {
        name: "user-configuration",
        components: {
            addDepToExempt,
        },
        data() {
            return {
                // userData: [],
                rowParams: {
                    deptId: "",
                    departmentTitle: '',
                    edit: false,
                    id: '',
                    limitAmt: 0,
                    username: '',
                    year: "",
                    costType: '',
                    yearDate:null,
                },
                data: [
                    {
                        year: "",
                    }
                ],
                lprModalVisible: false,
                loading: true,
                total: 0,
                openModal: false,
                isModalOpen: false,
                pageNo: 1,
                dicttArr: ['exemptFeeType'],
                dataDict: {}, // 数据字典
                modalTitle: '',
                exemptFeeTypeDict: [],
                formValidate: {
                    user: '',
                    limitAmt: 0,
                    // costType:[],
                    year: "",
                    departmentTitle: '',
                    pageNumber: 1,
                    pageSize: 10,
                },
                columns: [
                    {
                        title: "操作",
                        slot: "action",
                        width: 120,
                        align: "center"
                    },
                    {
                        title: "部门",
                        key: "user",
                        align: "center"
                    },
                    {
                        title: "整体限额",
                        key: "limitAmt",
                        align: "center"
                    },
                    {
                        title: "整体限额剩余额度",
                        key: "surplusLimitAmt",
                        align: "center"
                    },
                    {
                        title: "费用类型",
                        key: "costType",
                        type: "text",
                        align: "center",
                        slot: "costType",
                    },
                    {
                        title: "年份",
                        key: "year",
                        align: "center"
                    },

                ],
            }
        },
        computed: {},
        mounted() {
            this.init();
        },
        methods: {
            init() {
                this.getList();
                // this.getAllUserData();
                this.getDictDataList();
            },

            getList() {
                this.loading = true;
                getDepConfigListData(this.formValidate).then(res => {
                    this.loading = false;
                    if (res.code === "0000") {
                        this.total = res.data.total;
                        let dateList = res.data.records;
                        for (let i = 0; i < dateList.length; i++) {
                            getDeptByDeptId(dateList[i].deptId).then(res => {
                                if (res.code === "0000") {
                                    dateList[i].user = res.data.title
                                }
                            });
                        }
                        this.data = dateList;
                    }
                    console.log(this.data, " this.data ")
                });
            },
            getDep() {
                dics.forEach(dic => {
                    if (value.includes(dic.value)) {
                        rtn.push(dic.title);
                    }
                })
                getDepConfigListData(this.formValidate).then(res => {
                    this.loading = false;
                    if (res.code === "0000") {
                        this.data = res.data.records;
                        this.total = res.data.total;
                    }
                });
            },
            getDictDataList() {
                getDictDataList(this.dicttArr).then(res => {
                    console.group("数据字典:");
                    console.log(this.dicttArr.join("  "));
                    console.log(JSON.parse(JSON.stringify(res)));
                    console.groupEnd();
                    this.dataDict = res;
                });
            },
            changePageSize(v) {
                this.formValidate.pageSize = v;
                this.getList();
            },
            changePage(v) {
                this.formValidate.pageNumber = v;
                this.getList();
            },
            getTitle(type, val) {
                let data = this.dataDict[type + "Dict"] || [];
                return getTitleFromDataDict(data, val);
            },
            // getAllUserData() {
            //     getAllUserData().then(res => {
            //         if (res.code === "0000") {
            //             console.log("res.data", res.data)
            //             this.userData = res.data
            //         }
            //     })
            // },
            // 弹窗
            edit(params) {
                console.log("params", params)
                if (params.id !== null && params.id !== undefined) {
                    this.rowParams = deepClone(params)
                    this.rowParams.edit = true;
                    this.modalTitle = '编辑'
                    getDeptByDeptId(this.rowParams.deptId).then(res => {
                        if (res.code === "0000") {
                            console.log("res.data", res.data)
                            this.rowParams.departmentTitle = res.data.title
                            this.rowParams.deptId = res.data.id
                            this.rowParams.yearDate = this.rowParams.year
                            console.log(this.rowParams.yearDate,"this.rowParams.yearDate")
                            console.log(this.formValidate.year,"this.formValidate.year")
                            console.log("res.data.title", this.rowParams.departmentTitle)
                            this.isModalOpen = true;
                        }
                    });

                }

            },
            add(type) {
                if (type === 'add') {
                    this.modalTitle = '新增'
                    this.rowParams.edit = false;
                    this.rowParams.limitAmt = 0;
                    this.rowParams.username = '';
                    this.rowParams.year = "";
                    this.rowParams.yearDate = "";
                    this.rowParams.costType = '';
                    this.rowParams.departmentTitle = '';
                    this.rowParams.deptId = '';
                    this.isModalOpen = true;
                }
            },
            getDeptByDeptId(param) {
                getDeptByDeptId(this.rowParams.deptId).then(res => {
                    if (res.code === "0000") {
                        console.log("res.data", res.data)
                        this.rowParams.departmentTitle = res.data.title
                        this.rowParams.deptId = res.data.id
                        console.log("res.data.title", this.rowParams.departmentTitle)
                    }
                });
            },
            closeModal() {

                this.formValidate.pageNumber = 1;
                this.formValidate.pageSize = 10;
                this.isModalOpen = false;
                this.rowParams.id = '';
                this.getList();
            },
            remove(v) {
                this.$Modal.confirm({
                    title: "确认删除",
                    content: "是否确认删除？",
                    onOk: () => {
                        deleteExemptConfig(v.id).then(res => {
                            if (res.code === "0000") {
                                this.$Message.success("删除成功");
                                this.getList();
                            }
                        });
                    }
                });
            },
        },

    };
</script>
